package com.iuv.shortmsgnotice.controller;

import com.google.gson.Gson;
import com.iuv.shortmsgnotice.common.CommonOperateCode;
import com.iuv.shortmsgnotice.domain.DemoSmsExpressTbl;
import com.iuv.shortmsgnotice.service.DemoSmsExpressService;
import com.iuv.shortmsgnotice.util.RespondUtil;
import com.iuv.shortmsgnotice.util.SmsUtil;
import com.sun.xml.bind.v2.TODO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * ExpressController
 *
 * @author Will
 * @date 15:18
 */
@RequestMapping("express")
@RestController
public class ExpressController {
    /**
     * 签名模板
     */
    private static String SIGN_NAME = "深圳市艾优威科技";
    /**
     * IUV通讯云快递样例通知模板
     * 尊敬的${name}用户，您的取件码是：${code}，请不要把取件码泄露给其他人。
     */
    private static String EXPRESS_TEMPLATE_CODE = "SMS_202552258";

    @Autowired
    private DemoSmsExpressService demoSmsExpressService;

    /**
     * 快递列表搜索
     *
     * @param demoSmsExpressTbl
     * @param pageable
     * @return
     */
    @RequestMapping("listSearch")
    @ResponseBody
    public Map<String, Object> listSearch(DemoSmsExpressTbl demoSmsExpressTbl, Pageable pageable) {
        Map<String, Object> result =RespondUtil.respondSuccess("", "");
        result.put("data",demoSmsExpressService.listSearch(demoSmsExpressTbl, pageable));
        return result;
    }

    /**
     * 快递入库
     *
     * @param customerName
     * @param mobile
     * @return
     */
    @RequestMapping("enterStore")
    @ResponseBody
    public Map<String, Object> enterStore(String customerName, String mobile) {
        if (StringUtils.isBlank(customerName)) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.EXPRESS_CUSTOMER_NOT_NULL,
                    CommonOperateCode.SysMsg.EXPRESS_CUSTOMER_NOT_NULL);
        }
        if (StringUtils.isBlank(mobile)) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.EXPRESS_MOBILE_NOT_NULL,
                    CommonOperateCode.SysMsg.EXPRESS_MOBILE_NOT_NULL);
        }
        DemoSmsExpressTbl demoSmsExpressTbl = demoSmsExpressService.enterStore(customerName, mobile);
        //填充模板参数
        Map<String, Object> templateParamMap = new HashMap<>();
        templateParamMap.put("name", demoSmsExpressTbl.getCustomerName());
        templateParamMap.put("code", demoSmsExpressTbl.getPickUpCode());
        String templateParam = "";
        /**
         * TODO 短信推广 问题一 [try catch 的使用，对 Gson 序列化时的异常进行捕获]
         * 使用google的GSON将templateParamMap序列化为模板字符串
         * 当发生问题时，捕获异常，并抛出异常栈
         */
        try {
            Gson gson = new Gson();
            templateParam = gson.toJson(templateParamMap);
        } catch (Exception e) {
            throw new RuntimeException("短信模板参数序列化失败");
        }


        /**
         * end
         */

        //发送短信通知
        SmsUtil.sendSms(mobile, SIGN_NAME, EXPRESS_TEMPLATE_CODE, templateParam);
        return RespondUtil.respondSuccess(CommonOperateCode.SysCode.EXPRESS_ENTER_STORE_SUCCESS,
                CommonOperateCode.SysMsg.EXPRESS_ENTER_STORE_SUCCESS);
    }

    /**
     * 取件登记
     *
     * @param expressId
     * @return
     */
    @RequestMapping("pickUp")
    @ResponseBody
    public Map<String, Object> pickUp(Integer expressId) {


        if(expressId!=null) {
            DemoSmsExpressTbl demoSmsExpressTbl = demoSmsExpressService.pickUp(expressId);
            if (demoSmsExpressTbl != null) {
                return RespondUtil.respondSuccess(CommonOperateCode.SysCode.EXPRESS_PICK_UP_SUCCESS,
                        CommonOperateCode.SysMsg.EXPRESS_PICK_UP_SUCCESS);
            }
        }
        return RespondUtil.respondFail(CommonOperateCode.SysCode.EXPRESS_PICK_UP_FAIL,
                CommonOperateCode.SysMsg.EXPRESS_PICK_UP_FAIL);
    }
}
